package ArrayList;

import domain.Student1;

import java.util.ArrayList;
import java.util.Scanner;

/*
1.定义一个Student1类型的集合
2.为该集合添加元素
3.遍历集合选出符合条件的元素
4.将符合要求的元素放到新开辟的集合当中
5.返回这个集合并打印
 */
public class SelectElements {
    public static void main(String[] args){
        //1.定义一个Student1类型的集合
        ArrayList<Student1> list = new ArrayList<>();
        //2.为该集合添加元素调用写好的函数
        Student1 stu1 = InputStudent();
        Student1 stu2 = InputStudent();
        Student1 stu3 = InputStudent();
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);
        //5.返回这个集合并打印
        list = Result(list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println((list.get(i)).getName()+"******>"+(list.get(i)).getAge());
        }
    }
    public static Student1 InputStudent(){
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        int age = sc.nextInt();
        Student1 stu = new Student1(name,age);
        return stu;
    }
    public static ArrayList<Student1> Result(ArrayList<Student1> list){
        ArrayList<Student1> RList = new ArrayList<>();
        //3.遍历集合选出符合条件的元素
        //4.将符合要求的元素放到新开辟的集合当中
        for (int i = 0; i < list.size(); i++) {
            int CAge = (list.get(i).getAge());
            if (CAge < 18) {
                RList.add(list.get(i));
            }
        }
        return RList;
    }
}
